#!/usr/bin/python
# -*- coding: UTF-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
from ui.class_dialog import Ui_class_dialog


class view_class_window(QtWidgets.QDialog):
    ok_signal = QtCore.pyqtSignal(list)

    def __init__(self):
        QtWidgets.QDialog.__init__(self)
        self.ui = Ui_class_dialog()
        self.ui.setupUi(self)
        self.ui.ok_btn.clicked.connect(self.__ok_btn)
        self.ui.cancel_btn.clicked.connect(self.__cancel_btn)
        self.model_list = []

    def loadData(self,model_list):
        self.model_list = model_list
        _translate = QtCore.QCoreApplication.translate
        idx = 0
        for sub_model in model_list:
            modelHWidget = QtWidgets.QWidget(self.ui.scrollWidget)
            modelHWidget.setGeometry(QtCore.QRect(20, (idx * 30) + 10, 281, 41))
            modelHWidget.setObjectName(sub_model["uuid"] + "_hWidget")
            modelHLayout = QtWidgets.QHBoxLayout(modelHWidget)
            modelHLayout.setContentsMargins(0, 0, 0, 0)
            modelHLayout.setObjectName(sub_model["uuid"] + "_hLayout")

            modelLabel = QtWidgets.QLabel(modelHWidget)
            modelLabel.setObjectName(sub_model["uuid"] + "_label")
            modelLabel.setText(QtCore.QCoreApplication.translate("MainWindow", sub_model["file_name"]+":"))
            modelHLayout.addWidget(modelLabel)
            self.ui.scrollLayout.addWidget(modelHWidget)
            font = modelLabel.font()  # 获取当前字体
            font.setBold(True)  # 设置字体为粗体
            modelLabel.setFont(font)  # 应用新的字体设置
            idx = idx+1
            for sub_cls in sub_model["class_list"]:
                hWidget = QtWidgets.QWidget(self.ui.scrollWidget)
                hWidget.setGeometry(QtCore.QRect(20, (idx * 30) + 10, 281, 41))
                hWidget.setObjectName(sub_model["uuid"] + "_hWidget")
                horizontalLayout = QtWidgets.QHBoxLayout(hWidget)
                horizontalLayout.setContentsMargins(0, 0, 0, 0)
                horizontalLayout.setObjectName(sub_model["uuid"] + "_hLayout")

                label = QtWidgets.QLabel(hWidget)
                label.setObjectName(sub_model["uuid"] + "_label")
                horizontalLayout.addWidget(label)

                radioButton_0 = QtWidgets.QRadioButton(hWidget)
                radioButton_0.setObjectName(sub_cls["uuid"] + "_btn0")
                horizontalLayout.addWidget(radioButton_0)

                radioButton_1 = QtWidgets.QRadioButton(hWidget)
                radioButton_1.setObjectName(sub_cls["uuid"]+"_btn1")
                horizontalLayout.addWidget(radioButton_1)

                radioButton_2 = QtWidgets.QRadioButton(hWidget)
                radioButton_2.setObjectName(sub_cls["uuid"]+"_btn2")
                horizontalLayout.addWidget(radioButton_2)

                radioButton_3 = QtWidgets.QRadioButton(hWidget)
                radioButton_3.setObjectName(sub_cls["uuid"]+"_btn3")
                radioButton_3.setChecked(True)
                horizontalLayout.addWidget(radioButton_3)

                radio_group = QtWidgets.QButtonGroup(self.ui.scrollWidget)
                radio_group.setObjectName(sub_cls["uuid"] + "_group")
                radio_group.addButton(radioButton_0)
                radio_group.addButton(radioButton_1)
                radio_group.addButton(radioButton_2)
                radio_group.addButton(radioButton_3)
                self.ui.scrollLayout.addWidget(hWidget)
                class_lable_name = sub_cls["class_name"]
                label.setText(QtCore.QCoreApplication.translate("MainWindow", class_lable_name))
                radioButton_0.setText(_translate("class_dialog", "不使用"))
                radioButton_1.setText(_translate("class_dialog", "必选"))
                radioButton_2.setText(_translate("class_dialog", "包含"))
                radioButton_3.setText(_translate("class_dialog", "排除"))

                if sub_cls["used"] == 1:
                    radioButton_1.setChecked(True)
                elif sub_cls["used"] == 2:
                    radioButton_2.setChecked(True)
                elif sub_cls["used"] == 3:
                    radioButton_3.setChecked(True)
                else:
                    radioButton_0.setChecked(True)
                idx = idx + 1
        # 生成分类列表
        self.show()

    def __ok_btn(self):
        children_list = self.ui.scrollWidget.children()
        checked_dict = {}
        for idx, children in enumerate(children_list):
            if "QButtonGroup" in str(type(children)):
                uuid = children.objectName().replace("_group", "")
                checked_val = children.checkedButton().objectName().replace(uuid,"")
                val = 0
                if "_btn1" == checked_val:
                    val = 1
                elif "_btn2" == checked_val:
                    val = 2
                elif "_btn3" == checked_val:
                    val = 3
                checked_dict[uuid] = val
        for model in self.model_list:
            model["used"] = 0
            for cls in model["class_list"]:
                uuid = cls["uuid"]
                if uuid in checked_dict:
                    used = checked_dict[uuid]
                    cls["used"] = used
                    if used > 0:
                        model["used"] = 1
        self.ok_signal.emit(self.model_list)
        self.destroy()

    def __cancel_btn(self):
        self.destroy()


